$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'purchase/storematerialsupplier/list',
        datatype: "json",
        colModel: [
			{ label: 'ID', name: 'msId', index: 'ms_id', width: 40, key: true,align:'center'},
			{ label: '采购供应商名称', name: 'msName', index: 'ms_name', width: 120 },
            { label: '审核状态', name: 'status', index: 'status', width: 60 , align:'center', hidden:true},
			{ label: '审核状态',  width: 60 , align:'center',
                formatter: function (value, options, row) {
					var valueText = "";
                    var materialSupplierStatusEnum = $("#jqGrid").jqGrid('getGridParam', 'userData').materialSupplierStatusEnum;
                    $.each(materialSupplierStatusEnum, function (index, module) {
                        if (module.id == row.status) {
                            valueText = module.describe;
                            return false;
                        }
                    });
                    return valueText;
				}
			},
			{ label: '地址', name: 'address', index: 'address', width: 80 }, 			
			{ label: '联系人', name: 'contacts', index: 'contacts', width: 80 }, 			
			{ label: '联系电话', name: 'tel', index: 'tel', width: 80 }, 			
			{ label: '备注', name: 'remark', index: 'remark', width: 80 }, 			
			{ label: '审核人', name: 'auditName', index: 'audit_name', width: 80 }, 			
			{ label: '审核时间', name: 'auditTime', index: 'audit_time', width: 80 }, 			
			{ label: '更新人', name: 'updateName', index: 'update_name', width: 80 }, 			
			{ label: '更新时间', name: 'updateTime', index: 'update_time', width: 80 }, 			
			{ label: '创建人', name: 'createName', index: 'create_name', width: 80 },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }			
        ],
		viewrecords: true,
        height: window.innerHeight - 120,
        rowNum: 10,
		rowList : [25,50,100],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            vm.e.materialSupplierStatusEnum = $("#jqGrid").jqGrid('getGridParam', 'userData').materialSupplierStatusEnum;
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        formVisible: false,
		title: null,
		storeMaterialSupplier: {},
		e: {
            materialSupplierStatusEnum:null
		},
        q: {
            msName:null
        },
        form: {
            remark: null
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.storeMaterialSupplier = {};
		},
		save: function (event) {
            if(vm.validator()){
                return ;
            }
			$.ajax({
				type: "POST",
			    url: baseURL + "purchase/storematerialsupplier/save",
                contentType: "application/json",
			    data: JSON.stringify(vm.storeMaterialSupplier),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getInfo: function(msId){
			$.get(baseURL + "purchase/storematerialsupplier/info/"+msId, function(r){
                vm.storeMaterialSupplier = r.storeMaterialSupplier;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{
                    'msName': vm.q.msName
                },
            }).trigger("reloadGrid");
		},
        reset: function () {
            vm.q.msName = '';
            vm.reload();
        },
        validator: function () {
            if(isBlank(vm.storeMaterialSupplier.msName)){
                alert("采购供应商名称不能为空");
                return true;
            }
        },
        audit: function (msId) {
            var rowData = $("#jqGrid").jqGrid("getRowData",msId);
            vm.storeMaterialSupplier = rowData;
            vm.formVisible = true;
        },
        auditConfrim: function () {
            $.ajax({
                type: "GET",
                url: baseURL + "purchase/storematerialsupplier/auditConfrim",
                contentType: "application/json",
                data: {'msId':vm.storeMaterialSupplier.msId,'remark':vm.form.remark},
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功');
                        vm.formVisible = false;

                        vm.storeMaterialSupplier = {};
                        vm.form = {};
                        vm.reload();
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reject: function () {
            $.ajax({
                type: "GET",
                url: baseURL + "purchase/storematerialsupplier/reject",
                contentType: "application/json",
                data: {'msId':vm.storeMaterialSupplier.msId,'remark':vm.form.remark},
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.formVisible = false;
                            vm.storeMaterialSupplier = null;
                            vm.form = {};
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        }
	}
});